package cn.zyb.pet.service.impl;

import cn.zyb.basic.service.IBaiduAiAuditService;
import cn.zyb.basic.service.Impl.BaseServiceImpl;
import cn.zyb.basic.util.LoginContext;
import cn.zyb.org.domain.Employee;
import cn.zyb.org.mapper.EmployeeMapper;
import cn.zyb.pet.domain.Pet;
import cn.zyb.pet.domain.PetDetail;
import cn.zyb.pet.domain.PetOnlineAuditLog;
import cn.zyb.pet.mapper.PetDetailMapper;
import cn.zyb.pet.mapper.PetMapper;
import cn.zyb.pet.mapper.PetOnlineAuditLogMapper;
import cn.zyb.pet.service.IPetService;
import cn.zyb.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
  @Autowired
  private IBaiduAiAuditService baiduAiAuditService;

  @Autowired
  private PetMapper petMapper;

  @Autowired
  private PetDetailMapper petDetailMapper;

  @Autowired
  private EmployeeMapper employeeMapper;

  @Autowired
  private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

  @Override
  public void add(Pet pet) {
    super.add(pet);//自增id
    if(pet.getPetDetail()!=null){
      PetDetail petDetail = pet.getPetDetail();
      petDetail.setPetId(pet.getId());
      petDetailMapper.save(petDetail);
    }
  }

  @Override
  public void del(Long id) {
    super.del(id);
    //根据宠物的id删除宠物详情
    petDetailMapper.removeByPetId(id);
  }

  @Override
  public void update(Pet pet) {
    super.update(pet);
    if(pet.getPetDetail()!=null){
      PetDetail petDetail = pet.getPetDetail();
      petDetail.setPetId(pet.getId());
      //宠物详情更新：根据宠物详情id去更新
      petDetailMapper.update(petDetail);
    }
  }

  @Override
  public void onsale(List<Long> ids, HttpServletRequest request) {
    for (Long id : ids) {
      Pet pet = petMapper.loadById(id);
      //1.审核
      //审核文本
      Boolean boo1 = baiduAiAuditService.textAudit(pet.getName());
      //审核图片
      Boolean boo2 =baiduAiAuditService.imageAudit(pet.getResources());
      if(boo1 && boo2){//审核成功
        //a.设置上架时间
        pet.setOnsaletime(new Date());
        //修改状态：将状态由0 - 1
        pet.setState(1);
        petMapper.update(pet);

        //b.添加上架审核日志 - 审核成功的日志
        PetOnlineAuditLog log = new PetOnlineAuditLog();
        log.setPetId(id);
        log.setState(1);
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        log.setAuditId(employee.getId());
        log.setNote("审核成功");
        petOnlineAuditLogMapper.save(log);

      }else{//审核失败

        //添加上架审核日志 - 审核失败的日志
        PetOnlineAuditLog log = new PetOnlineAuditLog();
        log.setPetId(id);
        log.setState(0);
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        log.setAuditId(employee.getId());
        log.setNote("审核失败");
        petOnlineAuditLogMapper.save(log);
      }
    }
  }

  @Override
  public void offsale(List<Long> ids) {
    //修改状态：将状态由1 - 0
    petMapper.offsale(ids,new Date());
  }

}
